package com.yitong.equipment.utils

import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.yitong.equipment.App
import com.yitong.equipment.R
import com.yitong.equipment.interf.OnGlideUrlListener
import com.yitong.equipment.interf.OnGlideViewListener

object GlideUtils {
    fun loadImage(url: String?, imageView: ImageView?, isSquare: Boolean) {
        loadImageWithPlace(url, imageView, isSquare)
    }

    fun loadImage(res: Int, imageView: ImageView?) {
        Glide.with(App.instance)
            .load(res)
            .into(imageView!!)
    }

    fun loadImage(url: String?, imageView: ImageView?) {
        Glide.with(App.instance)
            .load(url)
            .into(imageView!!)
    }

    /**
     * 默认占位图
     */
    fun loadImageWithPlace(url: String?, imageView: ImageView?, isSquare: Boolean) {
        val requestOptions = RequestOptions()
        if (isSquare) {
            requestOptions.placeholder(R.drawable.holder_img_1)
                .dontAnimate()
                .error(R.drawable.holder_img_1)
        } else {
            requestOptions.placeholder(R.drawable.holder_img_2)
                .dontAnimate()
                .error(R.drawable.holder_img_2)
        }
        Glide.with(App.instance)
            .load(url)
            .apply(requestOptions)
            .into(imageView!!)
    }

    /**
     * 自定义错误颜色
     */
    fun loadImageWithError(url: String?, imageView: ImageView?, color: Int) {
        val requestOptions = RequestOptions()
            .placeholder(R.color.image_placeholder)
            .dontAnimate()
            .error(color)
        Glide.with(App.instance)
            .load(url)
            .apply(requestOptions)
            .into(imageView!!)
    }

    /**
     * 自定义占位图
     */
    fun loadImageWithPlace(url: String?, imageView: ImageView?, res: Int) {
        val requestOptions = RequestOptions()
            .dontAnimate()
            .placeholder(res)
        Glide.with(App.instance)
            .load(url)
            .apply(requestOptions)
            .into(imageView!!)
    }

    /**
     * 加载成功监听
     */
    fun loadWithViewListener(
        url: String?,
        imageView: ImageView?,
        onGlideViewListener: OnGlideViewListener
    ) {
        Glide.with(App.instance).load(url).into(object : SimpleTarget<Drawable?>() {
            override fun onResourceReady(
                resource: Drawable,
                transition: Transition<in Drawable?>?
            ) {
                imageView?.setImageDrawable(resource)
                onGlideViewListener.onGlideViewListener()
            }
        })
    }

    /**
     * 加载成功监听
     */
    fun loadWithUrlListener(url: String?, onGlideUrlListener: OnGlideUrlListener) {
        val requestOptions = RequestOptions()
            .skipMemoryCache(true)
        Glide.with(App.instance)
            .asBitmap()
            .load(url)
            .apply(requestOptions)
            .into(object : CustomTarget<Bitmap>(){
                override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                    onGlideUrlListener.onGlideUrlListener(resource)
                }
                override fun onLoadCleared(placeholder: Drawable?) {
                }
            })
    }
}